cat <<EOF > /etc/yum.repos.d/kubernetes.repo
[kubernetes]
name=Kubernetes
baseurl=http://mirrors.aliyun.com/kubernetes/yum/repos/kubernetes-el7-x86_64
enabled=1
gpgcheck=1
repo_gpgcheck=1
gpgkey=http://mirrors.aliyun.com/kubernetes/yum/doc/yum-key.gpg http://mirrors.aliyun.com/kubernetes/yum/doc/rpm-package-key.gpg
exclude=kube*
EOF

function docker_install()
{
	echo "检查Docker......"
	docker -v
    if [ $? -eq  0 ]; then
        echo "检查到Docker已安装!"
    else
    	echo "安装docker环境..."
        #curl -sSL https://get.daocloud.io/docker | sh
        echo "安装docker环境...安装完成!"
    fi
}
#安装docker,/etc/docker/daemon.json的修改，参考目录下的daemon修改，修改后记得执行systemctl dameon-reload，并重启docker
docker_install

systemctl disable firewalld
systemctl stop firewalld

# Set SELinux in permissive mode (effectively disabling it)
# 将 SELinux 设置为 permissive 模式(将其禁用)
setenforce 0
sed -i 's/^SELINUX=.*$/SELINUX=permissive/' /etc/selinux/config

#关闭swap
swapoff -a
#修改/etc/fstab中内容，将swap哪一行用#注释掉
sed -i 's/.*swap.*/#&/' /etc/fstab

#yum install -y kubelet-1.12.3 kubeadm-1.12.3 kubectl-1.12.3 --disableexcludes=kubernetes
yum install -y kubelet-1.17.4 kubeadm-1.17.4 kubectl-1.17.4 --disableexcludes=kubernetes


systemctl enable kubelet && systemctl start kubelet

cat <<EOF >  /etc/sysctl.d/k8s.conf
net.bridge.bridge-nf-call-ip6tables = 1
net.bridge.bridge-nf-call-iptables = 1
EOF
sysctl --system

# 下载k8s.1.17.4所需要的镜像列表,暂时注释掉

# 下载k8s.1.12.3所需要的镜像列表
echo 'docker pull registry.cn-hangzhou.aliyuncs.com/google_containers/kube-apiserver-amd64:v1.17.4
docker pull registry.cn-hangzhou.aliyuncs.com/google_containers/kube-controller-manager-amd64:v1.17.4
docker pull registry.cn-hangzhou.aliyuncs.com/google_containers/kube-scheduler-amd64:v1.17.4
docker pull registry.cn-hangzhou.aliyuncs.com/google_containers/kube-proxy-amd64:v1.17.4
docker pull registry.cn-hangzhou.aliyuncs.com/google_containers/etcd-amd64:3.4.3-0
docker pull registry.cn-hangzhou.aliyuncs.com/google_containers/pause:3.1
docker pull registry.cn-hangzhou.aliyuncs.com/google_containers/coredns:1.6.5

docker tag registry.cn-hangzhou.aliyuncs.com/google_containers/pause:3.1 k8s.gcr.io/pause:3.1
docker tag registry.cn-hangzhou.aliyuncs.com/google_containers/coredns:1.6.5 k8s.gcr.io/coredns:1.6.5
docker tag registry.cn-hangzhou.aliyuncs.com/google_containers/etcd-amd64:3.4.3-0 k8s.gcr.io/etcd:3.4.3-0
docker tag registry.cn-hangzhou.aliyuncs.com/google_containers/kube-scheduler-amd64:v1.17.4 k8s.gcr.io/kube-scheduler:v1.17.4
docker tag registry.cn-hangzhou.aliyuncs.com/google_containers/kube-controller-manager-amd64:v1.17.4 k8s.gcr.io/kube-controller-manager:v1.17.4
docker tag registry.cn-hangzhou.aliyuncs.com/google_containers/kube-apiserver-amd64:v1.17.4 k8s.gcr.io/kube-apiserver:v1.17.4
docker tag registry.cn-hangzhou.aliyuncs.com/google_containers/kube-proxy-amd64:v1.17.4 k8s.gcr.io/kube-proxy:v1.17.4' > ~/down-images.sh
chmod +777 ~/down-images.sh
sh ~/down-images.sh


sysctl net.bridge.bridge-nf-call-iptables=1

#kubeadm init --kubernetes-version=1.17.4 --image-repository registry.cn-hangzhou.aliyuncs.com/google_containers --pod-network-cidr=10.244.0.0/16 --apiserver-advertise-address=10.100.79.220
#10.244.0.0/16对应的是flannel的默认ip地址段，如果打算使用calico，这个值要改成192.168.0.0/16
#kubeadm init --kubernetes-version=1.17.4 --pod-network-cidr=10.244.0.0/16 --apiserver-advertise-address=10.100.79.220
kubeadm init --kubernetes-version=1.17.4 --pod-network-cidr=192.168.0.0/16 --apiserver-advertise-address=10.100.79.220

mkdir -p $HOME/.kube
sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
sudo chown $(id -u):$(id -g) $HOME/.kube/config
# 重要：必须安装一个pod网络附加扩展组件 ，我选择安装flanneld网络组件
#kubectl apply -f https://raw.githubusercontent.com/coreos/flannel/master/Documentation/kube-flannel.yml
#安装calico网络组件，没有选择flanneld
kubectl apply -f https://docs.projectcalico.org/v3.8/manifests/calico.yaml
#虚机里安装的时候，因为有2块网卡，所以不要直接安装，改为下载yaml后，修改配置，增加IP_AUTODETECTION_METHOD网卡指定
#wget https://docs.projectcalico.org/v3.8/manifests/calico.yaml

# 注意：备份类似以下格式的输出内容：用于节点加入使用
# kubeadm join 10.100.79.220:6443 --token qknvfe.v02ypyxnjvzjjzcs --discovery-token-ca-cert-hash sha256:18c361e1e5031ab1fb0c195b3dff6b2f3557c98db621cf34077afe66845e40ab
# 如生成的初始token无法使用，执行以下命令重新生成一个
# kubeadm token create --print-join-command
# 至此主节点k8s安装完成，你可以安装部署pod,或者其它扩展组件